package cn.ilikes.tools.nosql.jpa.clients.indexs;

import org.slf4j.Logger;


import cn.ilikes.tools.logs.Loggerfactory;
import cn.ilikes.tools.nosql.jpa.JPASQLPersistenUnitEnv;
import cn.ilikes.tools.nosql.jpa.clients.indexs.index.ABIndexSource;
import cn.ilikes.tools.nosql.jpa.clients.indexs.index.LuceneHadoopIndexSource;
import cn.ilikes.tools.nosql.jpa.clients.indexs.index.LuceneLocalIndexSource;
import cn.ilikes.tools.nosql.jpa.clients.indexs.index.lucenes.AsyLuceneClientIndex;

import cn.ilikes.tools.nosql.jpa.configs.indexs.IndexConfigs;

public class DefultClientIndexerFactory extends ABClientIndexerFactory {

	private static Logger logger = Loggerfactory.instance(DefultClientIndexerFactory.class);

	public DefultClientIndexerFactory(JPASQLPersistenUnitEnv persistenUnitEnv) {
		super(persistenUnitEnv);
	}

	@Override
	protected ABIndexSource createIndexSource(IndexConfigs indexConfigs) {
		Loggerfactory.info(logger, "create index source index configs "+indexConfigs.getName());
		ABIndexSource indexSource;
		switch (indexConfigs.getIndexType()) {
		case lucene: {
			indexSource = new LuceneLocalIndexSource(indexConfigs);
			break;
		}
		case asy_hadoop_lucene:
		case hadoop_lucene: {
			indexSource = new LuceneHadoopIndexSource(indexConfigs);
			break;
		}

		default:
			throw new ClientIndexingRunTimeException("no find type : " + indexConfigs.getIndexType().toString() + " Index  Source  ");
		}
		return indexSource;
	}

	@Override
	protected ABClientIndex createClientIndex(ABIndexSource indexSource) throws ClientIndexingException {
		
		Loggerfactory.info(logger, "create client  index  index source "+indexSource.toString());
		
		ABClientIndex clientIndex=null;
		switch (indexSource.getIndexConfigs().getIndexType()) {
		case lucene: {

			{
				//clientIndex = clientIndexs.get(indexSource);
				if (clientIndex == null) {
//					clientIndex = new SyncLuceneClientIndex(indexSource);
					
				}

			}
			break;
		}
		
		case asy_hadoop_lucene :
		{
			if (clientIndex == null) {
				  clientIndex = new AsyLuceneClientIndex(indexSource);
			}
			
			break;
		}
		
		case hadoop_lucene: {
			{
//				clientIndex = clientIndexs.get(indexSource);
				if (clientIndex == null) {
//					clientIndex = new SyncLuceneClientIndex(indexSource);
				}

			}
			break;
		}


		default:
			throw new ClientIndexingRunTimeException("no find type : " + indexSource.getIndexConfigs().getIndexType() + " Lucene Client Index  ");
		}
		return clientIndex;
	}

	@Override
	public void subClose() {
		
	}

}
